﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;

namespace ElectricDim
{
	public partial class WireNumDimForm : System.Windows.Forms.Form
	{
		public WireNumDimForm()
		{
			this.InitializeComponent();
		}

		public int DimStyle
		{
			get
			{
				if (!this.radioDigital.Checked)
				{
					return 1;
				}
				return 0;
			}
		}

		public int NumType
		{
			get
			{
				if (this.radioTwo.Checked)
				{
					return 0;
				}
				if (this.radioThree.Checked)
				{
					return 1;
				}
				return 2;
			}
		}

		public string Num
		{
			get
			{
				if (this.radioTwo.Checked)
				{
					return "2";
				}
				if (this.radioThree.Checked)
				{
					return "3";
				}
				return this.txtNum.Text.Trim();
			}
		}

		private void WireNumDimForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.InitPictureBox();
			this.ReadFormDataFromXml();
		}

		private void InitPictureBox()
		{
			string text = Path.Combine(Product.InstallLocation, "EquipmentImage", "ElectricLegend", "数字.png");
			if (!File.Exists(text))
			{
				this.pictureDigital.Image = this.pictureDigital.InitialImage;
			}
			else
			{
				this.pictureDigital.Image = Image.FromFile(text);
			}
			text = Path.Combine(Product.InstallLocation, "EquipmentImage", "ElectricLegend", "斜线.png");
			if (!File.Exists(text))
			{
				this.pictureLine.Image = this.pictureLine.InitialImage;
				return;
			}
			this.pictureLine.Image = Image.FromFile(text);
		}

		private void ReadFormDataFromXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(Path.Combine(Product.UserDataLocation, "HYElectricDim.xml"), "ElectricData");
			xmlHelper.SelectNode("WireNumDim");
			if (Convert.ToInt32(xmlHelper.GetAttributeEx("DimStyle", "0")) == 0)
			{
				this.radioDigital.Checked = true;
			}
			else
			{
				this.radioLine.Checked = true;
			}
			int num = Convert.ToInt32(xmlHelper.GetAttributeEx("NumType", "1"));
			if (num == 0)
			{
				this.radioTwo.Checked = true;
			}
			else if (num == 1)
			{
				this.radioThree.Checked = true;
			}
			else
			{
				this.radioMore.Checked = true;
			}
			string attributeEx = xmlHelper.GetAttributeEx("NumText", "4");
			this.txtNum.Text = attributeEx;
			int x = Convert.ToInt32(xmlHelper.GetAttributeEx("X", "100"));
			int y = Convert.ToInt32(xmlHelper.GetAttributeEx("Y", "100"));
			base.Location = new Point(x, y);
		}

		private void SaveFormDataToXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(Path.Combine(Product.UserDataLocation, "HYElectricDim.xml"), "ElectricData");
			xmlHelper.SelectNode("WireNumDim");
			xmlHelper.SetAttribute("DimStyle", this.DimStyle.ToString());
			xmlHelper.SetAttribute("NumType", this.NumType.ToString());
			xmlHelper.SetAttribute("NumText", this.txtNum.Text.Trim());
			xmlHelper.SetAttribute("X", base.Location.X.ToString());
			xmlHelper.SetAttribute("Y", base.Location.Y.ToString());
			xmlHelper.SaveXml("");
		}

		private void radioDigital_CheckedChanged(object sender, EventArgs e)
		{
			this.radioMore.Enabled = !this.radioLine.Checked;
			this.txtNum.Enabled = (this.radioMore.Enabled && this.radioMore.Checked);
			if (this.radioLine.Checked && this.radioMore.Checked)
			{
				this.radioThree.Checked = true;
			}
		}

		private void radioMore_CheckedChanged(object sender, EventArgs e)
		{
			this.radioLine.Enabled = !this.radioMore.Checked;
			this.txtNum.Enabled = (this.radioMore.Enabled && this.radioMore.Checked);
			if (this.radioMore.Checked && this.radioLine.Checked)
			{
				this.radioDigital.Checked = true;
			}
		}

		private void WireNumDimForm_Deactivate(object sender, EventArgs e)
		{
			string text = this.txtNum.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtNum.Text = this.txtNum.OldValue;
			}
			text = this.txtNum.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtNum.Text = "4";
			}
		}

		private void WireNumDimForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveFormDataToXml();
		}

		protected override void WndProc(ref Message msg)
		{
			if (msg.Msg == 274 && (int)msg.WParam == 61536)
			{
				base.Hide();
				return;
			}
			base.WndProc(ref msg);
		}

		private const string XmlFileName = "HYElectricDim.xml";

		private const string Xml_Root = "ElectricData";

		private const string XmlNode_Root = "WireNumDim";

		private const string XmlNode_DimStyle = "DimStyle";

		private const string XmlNode_NumType = "NumType";

		private const string XmlNode_NumText = "NumText";

		private const string LocationX = "X";

		private const string LocationY = "Y";
	}
}
